﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;

namespace FIM
{
    public partial class FormMain : Form
    {
        private ink.UserControl1 _ucBoard;
        private FormCapture _fmCap;

        public FormMain()
        {
            InitializeComponent();
            _ucBoard = new ink.UserControl1();
            _fmCap = new FormCapture();
            _fmCap.Hide();
            _fmCap.setRoot(this);
            pageBoard.Controls.Add(_ucBoard);
            _ucBoard.Dock = System.Windows.Forms.DockStyle.Fill;
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            _ucBoard.networkSwitch();
            cmdServerStart();
            System.Threading.Thread.Sleep(1500);
            _fmCap.cmdWmpCapPlay(getSelfIP());
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            _ucBoard.networkSwitch();

            cmdWmpStop();
            cmdServerStop();
        }

        private void cmdWmpPlay()
        {
            wmp.network.bufferingTime = 1000;
            wmp.URL = fConn.getVideoSource();
            wmp.Ctlcontrols.play();
            btVideoPlay.IsPressed = true;
        }

        private void cmdWmpStop()
        {
            wmp.Ctlcontrols.stop();
            btVideoPlay.IsPressed = false;
            btVideoPlay.Refresh();
        }

        private void ribbonButton3_Click(object sender, EventArgs e)
        {
            if (btVideoPlay.IsPressed)
                cmdWmpStop();
            else
                cmdWmpPlay();
        }

        private void ribbonButton2_Click(object sender, EventArgs e)
        {
            cmdWmpStop();
            cmdServerStop();
        }

        private Process _server;
        private void cmdServerStart()
        {
            string ip = getSelfIP();
            if (ip == null)
                return;

            _server = new Process();
            _server.StartInfo.FileName = @"vlc\vlc.exe";
            _server.StartInfo.Arguments = "dshow:// :dshow-vdev= :dshow-adev=none :dshow-size=320x240 :sout=#transcode{vcodec=WMV2,vb=800,fps=20,scale=1,width=320,height=240,acodec=none}:std{access=mmsh,mux=asfh,dst=" + ip + ":5004} -I --rc-fake-tty";
            _server.Start();
            btVideoServer.IsPressed = true;
        }

        private void cmdServerStop()
        {
            if (_server != null && !_server.HasExited)
                _server.Kill();
            _server = null;
            btVideoServer.IsPressed = false;
            btVideoServer.Refresh();
        }

        private void ribbonButton4_Click(object sender, EventArgs e)
        {
            if (btVideoServer.IsPressed)
                cmdServerStop();
            else
                cmdServerStart();
        }

        private static string getSelfIP()
        {
            string ipSelf = null;
            IPHostEntry ips = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in ips.AddressList)
                if (ip.ToString().Split('.').Length == 4)
                    ipSelf = ip.ToString();
            return ipSelf;
        }

        private void wmp_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            switch (e.newState)
            {
                case 1:
                    this.Text = "FIM - 視訊停止";
                    break;
                case 3:
                    this.Text = "FIM - 視訊播放中";
                    break;
                case 6:
                    this.Text = "FIM - 視訊緩衝中";
                    break;
                case 7:
                    this.Text = "FIM - 視訊連線中";
                    break;
            }
        }

        private void ribbonButton1_Click(object sender, EventArgs e)
        {
            fConn.Show();
        }

        private void fConn_Load(object sender, EventArgs e)
        {
            fConn.Hide();
        }

        private void btCapture_Click(object sender, EventArgs e)
        {
            _fmCap.cmdWmpCapPlay(getSelfIP());
            _fmCap.Show();
        }

        public void setScreenshot(Bitmap bmp)
        {
            Console.WriteLine(bmp);
            if (bmp == null) return;
            _ucBoard.setupBackgroundImage(bmp);
        }

    }
}
